package com.east.service.fee.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.DictionaryItems;
import com.east.bean.Fee;
import com.east.bean.Project;
import com.east.bean.Staff;
import com.east.controller.fee.info.Subject;
import com.east.dao.DictionaryItemsMapper;
import com.east.dao.FeeMapper;
import com.east.dao.ProjectMapper;
import com.east.dao.StaffMapper;
import com.east.exception.DescribeException;
import com.east.service.fee.FeeService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("feeService")
public class FeeServiceImpl implements FeeService{
	@Autowired 
	FeeMapper feeMapper;
	@Autowired 
	StaffMapper staffMapper;
	@Autowired 
	ProjectMapper projectMapper;
	@Autowired 
	DictionaryItemsMapper dictionaryItemsMapper;
	@Override
	public Page<Fee> findByPage(Pagenation pagenation, String key,String subject,String startTime ,String endTime) {
		Page<Fee> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		if(StringUtils.isBlank(endTime)) {
			endTime= DateUtils.getDaysOfDate(new Date());
		}
		feeMapper.finByFeeAll(key,subject,startTime,endTime);
		return startPage;
	}

	@Override
	public boolean saveFee(Fee fee) {
		Fee fe = new Fee();
		fe.setFrMan(fee.getFrMan());//科目
		fe.setProjectId(fee.getProjectId());//项目id
		fe.setFrSummary(fee.getFrSummary());//摘要
		fe.setFrUuid(UUID.randomUUID().toString());//唯一标识
		fe.setStaffId(fee.getStaffId());//经办人
		fe.setFrType(fee.getFrType());//付款类型
		fe.setRrAcount(fee.getRrAcount());//金额
		if(StringUtils.isNotBlank(fee.getDateTime())) {
			fe.setRrPayTime(DateUtils.getDate(fee.getDateTime()));
		}
		fe.setProjectId(fee.getProjectId());
		int insert = feeMapper.insert(fe);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateFee(Fee fee) {
		
		Fee fe = feeMapper.selectByPrimaryKey(fee.getFrId());
		fe.setFrId(fee.getFrId());
		fe.setFrMan(fee.getFrMan());//科目
		fe.setProjectId(fee.getProjectId());//项目id
		fe.setFrSummary(fee.getFrSummary());//摘要
		fe.setStaffId(fee.getStaffId());//经办人
		fe.setFrType(fee.getFrType());//付款类型
		fe.setRrAcount(fee.getRrAcount());//金额
		if(StringUtils.isNotBlank(fee.getDateTime())) {
			fe.setRrPayTime(DateUtils.getDate(fee.getDateTime()));
		}
		fe.setProjectId(fee.getProjectId());
		int updateByPrimaryKey = feeMapper.updateByPrimaryKey(fe);
		if(updateByPrimaryKey>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean delFee(Fee fee) {
		int deleteByPrimaryKey = feeMapper.deleteByPrimaryKey(fee.getFrId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}

	@Override
	public Fee findByOne(Fee fee) {
		return feeMapper.selectByPrimaryKey(fee.getFrId());
	}

	@Override
	public String saveListFee(List<Fee> fee) {
		for (Fee fe : fee) {
			fe.setFrUuid(UUID.randomUUID().toString());
			try {
				if(StringUtils.isNotBlank( fe.getStaffName())) {
				Staff findByName = staffMapper.findByName(fe.getStaffName());//经办人
				fe.setStaffId(findByName.getsId());
				}
			} catch (DescribeException e) {
				new DescribeException("经办人重名，请联系人事部门！！");
				return "经办人重名，请联系人事部门！！";
			}
			try {
				if(StringUtils.isNotBlank(fe.getSubjectName())) {
					DictionaryItems findByName = dictionaryItemsMapper.findByName( fe.getSubjectName());//科目名称
					fe.setFrMan(findByName.getDiId());
				}
			} catch (DescribeException e) {
				new DescribeException("科目重复,请查看基本设置科目管理");
				return "科目重复,请查看基本设置科目管理";
			}
			
			try {
				if(StringUtils.isNotBlank( fe.getProjectNum())) {
				Project findByName = projectMapper.findBySerialNum( fe.getProjectNum());//科目名称
				fe.setProjectId(findByName.getpId());
				}
			} catch (MyBatisSystemException e) {
				//new DescribeException("项目编号不唯一,请查看基本设置科目管理");
				Project findByNames = projectMapper.findBySerialNumAndName( fe.getProjectNum(),fe.getProjectName());//科目名称
				if(findByNames!=null) {
					fe.setProjectId(findByNames.getpId());
				}
				
			}
			try {
				DictionaryItems findByName = dictionaryItemsMapper.findByName(fe.getFrType());//付款类型
				fe.setFrType(findByName.getDiId().toString());
			} catch (DescribeException e) {
				new DescribeException("付款类型重复,请查看基本设置付款类型");
				return "付款类型重复,请查看基本设置付款类型";
			}
		}
		int insertList = feeMapper.insertList(fee);
		if(insertList>0) {
			return "添加成功";
		}else {
			return "添加失败";
		}
		
	}
	
	@Override
	public List<Subject> fingBySubject(String year, String month) {
		return feeMapper.findBySubject(year, month);
	}
	@Override
	public Page<Subject> findByProject(String key,Pagenation pagenation) {
		Page<Subject> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		feeMapper.findByProject(key);
		return startPage;
	}
	

}
